package com.medical.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.DataAnalyze;
import com.medical.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年11月13日 下午12:37:25 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/MyMessageMan")
public class MyMessageController {
	/**
	 * 我接收的消息管理界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/MyReceivedMessage")
	public ModelAndView MyMessageMan()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/MyMessageMan/MyReceivedMessage");
		return model;
	}
	/**
	 * 我的消息管理的tab异步加载
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/MyReceivedMessageAjax")
	public @ResponseBody Map<String,Object> MyReceivedMessageAjax(int limit,int offset,String type,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+hosploginid);
		
		String OrderParam = "limit="+limit+"&offset="+offset+"&type="+type+"&hosploginid="+hosploginid;//参数串
		
		JSONObject MessageListjson = HttpRequest.sendPost("listreceivenotification",OrderParam);
		System.out.println("输出接收到的json数据是："+MessageListjson);
		
		String jsonobjectmsg = MessageListjson.get("msg").toString();
		String jsonobjectcode =MessageListjson.get("code").toString();
		
		List<Map<String,Object>> orderlist=new ArrayList<>();
		Integer total=0;
		try {
			JSONObject jsonobjectdata =MessageListjson.getJSONObject("data");
			JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
			total = Integer.parseInt(jsonobjectdata.get("total").toString());
			orderlist = DataAnalyze.MessageJsonToList(jsonarrayrows);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("数据解析出错了，出错信息是："+e.getMessage());
		}
		
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", orderlist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
        
		return map;
	}
	/**
	 * 我发送的消息界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/MySendedMessage")
	public ModelAndView MySendedMessage()throws Exception{
		ModelAndView model  = new ModelAndView();
		model.setViewName("MainFrame/MyMessageMan/MySendedMessage");
		return model;
	}
	
	@RequestMapping(value="/MySendedMessageAjax")
	public @ResponseBody Map<String,Object> MySendedMessageAjax(int limit,int offset,String type,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+hosploginid);
		
		String OrderParam = "limit="+limit+"&offset="+offset+"&type="+type+"&hosploginid="+hosploginid;//参数串
		
		JSONObject MessageListjson = HttpRequest.sendPost("listsendernotification",OrderParam);
		System.out.println("输出接收到的json数据是："+MessageListjson);
		
		String jsonobjectmsg = MessageListjson.get("msg").toString();
		String jsonobjectcode =MessageListjson.get("code").toString();
		
		List<Map<String,Object>> orderlist=new ArrayList<>();
		Integer total=0;
		try {
			JSONObject jsonobjectdata =MessageListjson.getJSONObject("data");
			JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
			total = Integer.parseInt(jsonobjectdata.get("total").toString());
			orderlist = DataAnalyze.MessageJsonToList(jsonarrayrows);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("数据解析出错了，出错信息是："+e.getMessage());
		}
		
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", orderlist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
        
		return map;
	}
	
	@RequestMapping(value="/test")
	public ModelAndView test()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/test/datetimeTest");
		return model;
	}
	
	/**
	 * 将未读消息置为已读
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/Readnotification")
	public @ResponseBody Map<String,Object> Readnotification(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		Integer notificationid = Integer.parseInt(request.getParameter("notificationid"));
		
		String param = "hosploginid="+hosploginid+"&notificationid="+notificationid;
		Map<String,Object> result = new HashMap<>();
		String jsoncode = "";
		String jsonmsg = "";
		try {
			JSONObject json = HttpRequest.sendPost("readnotification", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
			jsoncode="-100";
			jsonmsg="数据解析出错";
		}
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		
		return result;
	}
	
	/**
	 * 删除接收到的消息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/Deletereceivenotification")
	public @ResponseBody Map<String,Object> Deletereceivenotification(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		Integer notificationid = Integer.parseInt(request.getParameter("notificationid"));
		
		String param = "hosploginid="+hosploginid+"&notificationid="+notificationid;
		Map<String,Object> result = new HashMap<>();
		String jsoncode = "";
		String jsonmsg = "";
		try {
			JSONObject json = HttpRequest.sendPost("deletereceivenotification", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
			jsoncode="-100";
			jsonmsg="数据解析出错";
		}
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		
		return result;
	}
}
